﻿using CPPEI.ETL.DataServiceInterface;
using CPPEI.WebApi;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using ENT = CPPEI.ETL.Domain.Entities;
using MOD = CPPEI.ETL.Domain.Models;
using SER = CPPEI.ETL.Domain.Services;

namespace CPPEI.ETL.WebApi.DtSer
{
    /// <summary>
    /// 测试数据源的数据，得到数据源的结构
    /// </summary>
    [CPPEI.WebApi.AuthPostAction("ETL", "TestDataSourceColumns", "1.0")]
    public class TestDataSourceColumnsColumnsAction : BaseAction<TestDataSourceColumnsParams, TestDataSourceColumnsResult>
    {
        protected override ActionResult<TestDataSourceColumnsResult> OnExecute(ActionContext context, 
            SysParameters sysParameters,
            TestDataSourceColumnsParams opParams)
        {
            SER.DtSerService service = new SER.DtSerService();
            ListResult<Column> serResult = service.GetDataSourceSchema(opParams.ServiceId, opParams.SourceId, opParams.Parameters);

            TestDataSourceColumnsResult result = new TestDataSourceColumnsResult();

            if (serResult.IsSuccess)
            {
                foreach (var column in serResult.Data)
                {
                    TestDataSourceColumnsResult.Item item = new TestDataSourceColumnsResult.Item();
                    item.Name = column.Name;
                    item.Type = column.TypeFullName;
                    result.Items.Add(item);
                }

                return new ActionResult<TestDataSourceColumnsResult>(result);
            }
            else
            {
                return new ActionResult<TestDataSourceColumnsResult>(serResult.IsSuccess, serResult.ErrorCode, serResult.Message, null);
            }
        }
    }

    public class TestDataSourceColumnsParams
    {
        [Parameter]
        public Guid ServiceId { get; set; }

        [Parameter]
        public Guid SourceId { get; set; }

        [Parameter]
        public string Parameters { get; set; }
    }

    public class TestDataSourceColumnsResult
    {
        public List<Item> Items = new List<Item>();

        public class Item
        {
            public string Name { get; set; }
            public string Type { get; set; }
        }
    }
}
